package com.findme.fecedetection.ui.fragment

import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.findme.fecedetection.Const
import com.findme.fecedetection.R
import com.findme.fecedetection.adapter.HomeAdapter
import com.findme.fecedetection.base.LazyFragment
import com.findme.fecedetection.bean.PetListBean
import com.findme.fecedetection.net.HttpManager
import com.findme.fecedetection.ui.window.OnePickerViewPopupWindow
import com.findme.fecedetection.utlis.getKeysList
import com.findme.fecedetection.utlis.getValuesList
import com.tanguy.user.utlis.extend.*
import org.jetbrains.anko.find

/**
 * Created by dab on 2017/11/19 0019 16:33
 */
class FindFragment: LazyFragment() {
    override fun viewLayoutID(): Int = R.layout.fragment_find
    private var type=1//寻找2领回
    private var data = ArrayList<PetListBean>()
    private val homeAdapter = HomeAdapter(data)
    override fun onFirstVisibleToUser(view: View?) {
        view?.apply {
            click(R.id.tv_find) {
                find<View>(R.id.view_line).visibility=View.VISIBLE
                find<View>(R.id.view_line_2).visibility=View.GONE
                type=1
                refresh(this)
                HttpManager.getPage("", "", type.toString(), "", "", "", "")
                        .requestSucceed(this@FindFragment) {
                            homeAdapter.setNewData(it.list)
                        }
            }
            click(R.id.tv_take_back) {
                find<View>(R.id.view_line).visibility=View.GONE
                find<View>(R.id.view_line_2).visibility=View.VISIBLE
                type=2
                refresh(this)
                HttpManager.getPage("", "", type.toString(), "", "", "", "")
                        .requestSucceed(this@FindFragment) {
                            homeAdapter.setNewData(it.list)
                        }
            }
            find<RecyclerView>(R.id.rv_content).apply {
                adapter=homeAdapter
                layoutManager = LinearLayoutManager(activity)
            }
            click(R.id.tv_color){
                refresh(this)
                activity.showPopupWindow(OnePickerViewPopupWindow(activity, getValuesList(Const.Type.CWYS),{
                    pos, msg ->
                    setText(R.id.tv_color,msg!!)
                    val s = getKeysList(Const.Type.CWYS)[pos]
                    loge(s)
                    HttpManager.getPage(s, "", type.toString(), "", "", "", "")
                            .requestSucceed(this@FindFragment) {
                                homeAdapter.setNewData(it.list)
                            }
                }))
            }
            click(R.id.tv_size){
                refresh(this)
                activity.showPopupWindow(OnePickerViewPopupWindow(activity, getValuesList(Const.Type.CWDX),{
                    pos, msg ->
                    setText(R.id.tv_size,msg!!)
                    val s = getKeysList(Const.Type.CWYS)[pos]
                    HttpManager.getPage("", "", type.toString(), s, "", "", "")
                            .requestSucceed(this@FindFragment) {
                                homeAdapter.setNewData(it.list)
                            }
                }))
            }
        }
        HttpManager.getPage("", "", type.toString(), "", "", "", "")
                .requestSucceed(this@FindFragment) {
                    homeAdapter.setNewData(it.list)
                }

    }

    fun refresh(view: View?) {
        view?.apply {
            setText(R.id.tv_color,"颜色")
            setText(R.id.tv_size,"大小")
            setText(R.id.tv_category,"品种")
        }

    }
}